<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>生活服务充值</title>
	</head>
	<body>
		<label >电话号码
			<input type="text" name="phoneNumber" id="phoneNumber"  placeholder="输入电话号" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
		</label>
		<label >券
			<select name="coupon" id="coupon">
				<option value="None">None</option>
			</select>
			<select  id="scene" onchange="toggleAmountInput()" >
				<option value="1">话费</option>
				<option value="2">电子钱包</option>
				<option value="3">生活服务支付</option>
				<option value="4">qirs支付</option>
				<option value="5">hematpay</option>
			</select>
		</label>
		<label >
			<input type="text"  id="amount" oninput="this.value = this.value.replace(/[^0-9]/g, '');" style="display: none;">金额
			<input type="radio" name="element"  value="pre"  checked>pre
			<input type="radio" name="element"  value="sit">sit
			<input type="button"  id="btn" value="确认">
		</label><br />


		<button  type="submit" id="sumbit" disabled>提交</button>


		   <script src='https://code.jquery.com/jquery-3.6.0.min.js'></script>
		   <script>
			   var typingTimer;  // 定义一个计时器变量
		       var doneTypingInterval = 2000;  // 设置延迟时间为2秒


			   function toggleAmountInput() {
			           var scene = $("#scene").val();
			           if (scene === '4' || scene === '5') {
			               $("#amount").show();
			           } else {
			               $("#amount").hide();
			           }
			       }


			   function ceshi(){

			   					   $("#btn").click(function(){
									   var Element = $("input[name='element']:checked").val();
									   var keyword = $("#phoneNumber").val()
									   var scene=$("#scene").val()
									   var amount=$("#amount").val()
			   						   $.ajax({
			   							   type:"POST",
			   							   url:"/counpon",
			   							   contentType:"application/json",
			   							   data:JSON.stringify({
			   							   phone:keyword,
			   							   element:Element,
			   							   scene:scene,
			   							   amount:amount
			   							   }),
			   							   success:function(result){
											   $("#sumbit").attr("disabled",false)
			   								   console.log(result)
			   									if (result.code === 2005){
			   										alert("号码不存在")
			   									}
			   									else if (result.success == false){
			   										alert("后端错误，请检查号码或稍后重试")

			   									}
												else if (result.code ==20001){
													alert("无可用券")
												}
			   									else{
			   										var promotionAmount = result.data.availableList || [];
			   										console.log(promotionAmount)
			   										// for (var i=0; i<result.data.availableList.length; i++){
			   										// 	promotionAmount.push(result.data.availableList[i])
			   										// }
			   										
			   										var SelectElemet=$('#coupon')
													SelectElemet.empty()
													SelectElemet.append($('<option>',{
														value:'None',
														text:'None'
													}))

			   										$.each(promotionAmount,function(index,item){
			   											SelectElemet.append($('<option>',{
			   												value:item.couponSerialNo,
			   												text:item.promotionAmount
			   											}))

			   										})


			   									}

			   							   }


			   						   })
			   					   },)
			   				   }


			   $("input[name='element']").on('change',ceshi)

			   $("#phoneNumber").on('input', ceshi)

			   $("#sumbit").click(function(){
				   var phone = $("#phoneNumber").val()
				   var element=$("input[name='element']:checked").val();
				   var coupon=$("#coupon").val()
				   var scene=$("#scene").val()
				   $.ajax({
					   type:"POST",
					   url:"/query",
					   contentType:"application/json",
					   data:JSON.stringify({phone:phone, element:element, coupon:coupon,scene:scene}),
					   success:function(result){

						    if (result.code === 10000){
							   alert(result.message)
						   }
						   else if (result.code ===20000){
							   alert(result.message)
						   }
						   else{
							   alert("未知错误，支付失败")
						   }
						   ceshi()



					   },
					   error:function(result){
						   alert("未知错误，请检查服务")
					   }
					



				   })

			   })

		</script>


	</body>
</html>



